// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © julzen2

//@version=5
indicator(title="Weighted WCCI", shorttitle="Weighted WCCI", overlay=false)

//--- Input Parameters ---
TCCIp       = input.int(7, "Turbo CCI Period", minval=1)
CCIp        = input.int(13, "CCI Period", minval=1)
overbslevel = input.float(200.0, "Overbought/Oversold Level")
triglevel   = input.float(50.0, "Trigger Level")
weight      = input.float(1.0, "Weight")

//--- Pine Script Buffers (variables for plotting) ---
var float ExtMapBuffer1 = na // Turbo CCI (Red)
var float ExtMapBuffer2 = na // CCI (DodgerBlue)
var float ExtMapBuffer3 = na // CCI Histogram (CadetBlue)
var float ExtMapBuffer4 = na // Overbought Level (Red)
var float ExtMapBuffer5 = na // Oversold Level (Red)
var float ExtMapBuffer6 = na // Trigger Level (RoyalBlue)
var float ExtMapBuffer7 = na // Negative Trigger Level (RoyalBlue)
var float ExtMapBuffer8 = na // Zero Level (PaleGreen)

//--- Global variables for trend tracking (mimicking MQL4's trend_counter and trend string) ---
var int trend_counter = 0
var string trend = "ZERO"

//--- Main Calculation ---
// MQL4's iCCI(NULL,0,Period,PRICE_TYPICAL,shift) translates to ta.cci(hlc3, Period)[shift]
// MQL4's iATR(NULL,0,Period,shift) translates to ta.atr(Period)[shift]

// Calculate CCI and Turbo CCI for the current bar (shift = 0 in MQL4)
float currentCCI  = ta.cci(hlc3, CCIp)
float currentTCCI = ta.cci(hlc3, TCCIp)

// Calculate ATR for the current bar (shift = 0 in MQL4)
float atr7  = ta.atr(7)
float atr49 = ta.atr(49)

float Kw = 0.0

if weight == 0
    Kw := 0.0
else
    Kw := weight * (atr7 / atr49)
    currentCCI  := currentCCI * Kw
    currentTCCI := currentTCCI * Kw

// Clamp values as per MQL4 logic
if currentTCCI > overbslevel + 50
    currentTCCI := overbslevel + 50
if currentCCI > overbslevel + 50
    currentCCI := overbslevel + 50
if currentCCI < -overbslevel - 50
    currentCCI := -overbslevel - 50
if currentTCCI < -overbslevel - 50
    currentTCCI := -overbslevel - 50

// Assign values to plotting variables
ExtMapBuffer1 := currentTCCI
ExtMapBuffer2 := currentCCI
ExtMapBuffer3 := currentCCI // Used for histogram
ExtMapBuffer4 := overbslevel
ExtMapBuffer5 := -overbslevel
ExtMapBuffer6 := triglevel
ExtMapBuffer7 := -triglevel
ExtMapBuffer8 := 0.0

// --- Trend Logic (for comment only) ---
// Pine Script executes on every bar, so we handle previous bar state with `[1]` or `var` variables.
// The MQL4 `shift==0` logic applies to the current bar.
if barstate.islast
    // Let's replicate the MQL4 logic as closely as possible for the `trend` string and `trend_counter`.
    // It is important to note that MQL4's `trend` variable is a global.
    // In Pine, `var` ensures persistence across bars.

    if currentCCI > 0
        if trend[1] == "UP" // Using [1] to get the value from the *previous* bar
            trend_counter := trend_counter[1] + 1
        else
            trend_counter := 1
            trend := "UP"
    else if currentCCI < 0 // Assuming MQL4 implied `currentCCI < 0` for "DOWN"
        if trend[1] == "DOWN"
            trend_counter := trend_counter[1] + 1
        else
            trend_counter := 1
            trend := "DOWN"
    else // currentCCI == 0 or very close
        trend_counter := 1
        trend := "ZERO"

    // The MQL4 'Comment' function for status bar text is removed for Pine compatibility
    // You can see the `trend` and `trend_counter` values in the Data Window when hovering over the indicator.


//--- Plotting ---
// Plot 0: Turbo CCI
// Changed color.red to color.rgb(255, 0, 0)
plot(ExtMapBuffer1, title="Turbo CCI", color=color.rgb(255, 0, 0), style=plot.style_line, linewidth=1)

// Plot 1: CCI
// Changed color.dodgerBlue to color.rgb(30, 144, 255)
plot(ExtMapBuffer2, title="CCI", color=color.rgb(30, 144, 255), style=plot.style_line, linewidth=3)

// Plot 2: CCI Histogram
// Changed color.cadetBlue to color.rgb(95, 158, 160)
plot(ExtMapBuffer3, title="CCI Histogram", color=color.rgb(95, 158, 160), style=plot.style_columns)

// Plot 3: Overbought Level (Red)
// Changed color.red to color.rgb(255, 0, 0)
plot(ExtMapBuffer4, title="Overbought Level", color=color.rgb(255, 0, 0), style=plot.style_line, linewidth=1, show_last=5000)

// Plot 4: Oversold Level (Red)
// Changed color.red to color.rgb(255, 0, 0)
plot(ExtMapBuffer5, title="Oversold Level", color=color.rgb(255, 0, 0), style=plot.style_line, linewidth=1, show_last=5000)

// Plot 5: Trigger Level (RoyalBlue)
// Changed color.royalBlue to color.rgb(65, 105, 225)
plot(ExtMapBuffer6, title="Trigger Level (+)", color=color.rgb(65, 105, 225), style=plot.style_line, linewidth=1, show_last=5000)

// Plot 6: Negative Trigger Level (RoyalBlue)
// Changed color.royalBlue to color.rgb(65, 105, 225)
plot(ExtMapBuffer7, title="Trigger Level (-)", color=color.rgb(65, 105, 225), style=plot.style_line, linewidth=1, show_last=5000)

// Plot 7: Zero Level (PaleGreen)
// Changed color.paleGreen to color.rgb(152, 251, 152)
plot(ExtMapBuffer8, title="Zero Level", color=color.rgb(152, 251, 152), style=plot.style_line, linewidth=1, show_last=5000)
